package com.parcel;

/**
 * Created by zhuangqi on 2016/12/12.
 */
interface Selector {
    boolean end();
    Object current();
    void next();
}

public class Sequence {
    private Object[] items;
    private int next = 0;
    public Sequence(int size) {
        items = new Object[size];
    }
    //末尾增加新的Object
    public void add(Object x) {
        if (next < items.length)
            items[next++] = x;
    }

    //内部类访问其外围类的方法和字段
    private class SequenceSelector implements Selector {

        private int i = 0;

        public boolean end() {
            return i == items.length;
        }

        public Object current() {
            return items[i];
        }

        public void next() {
            if (i<items.length) i++;
        }
    }
    public Selector selector() {
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for (int i = 0; i < 10; i++) {
            //添加String对象
            sequence.add(Integer.toString(i));
            Selector selector = sequence.selector();
            while (!selector.end()) {
                //获取一个Selector
                System.out.println(selector.current() + " ");
                //向后移动
                selector.next();
            }
        }
    }
}
